/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.event;

import database.worker.DBEventWorker;
import dto.EXIFData;
import dto.Event;
import dto.GeoPoint;
import dto.Image;
import dto.ImageEvent;
import dto.TextEvent;
import dto.Track;
import dto.Video;
import dto.VideoEvent;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.ResourceBundle;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;
import map.MapManager;
import org.jdesktop.swingx.mapviewer.GeoPosition;
import util.MapUtils;

/**
 * The Window (Dialog) to create/edit an event
 * 
 * @author Pascal
 */
public class AddEditEventWindow extends JDialog {

    private static final int COMBO_TEXT_SELECTED_INDEX = 0;
    private static final int COMBO_PICTURE_SELECTED_INDEX = 1;
    private static final int COMBO_VIDEO_SELECTED_INDEX = 2;
    private boolean isEdit;
    private boolean textAreaVisible;
    private Track track;
    private Event event;
    private dto.Image image;
    private MapManager mapManager;
    private GeoPosition geoPosition;

    /**
     * Creates a new form to add an event to a track
     *
     * @param track the Track the event should belong to
     */
    public AddEditEventWindow(Track track) {
        this(false, null, track);
        setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/gui/resources/logo.png")));
    }

    /**
     * Creates a new form to edit an event
     *
     * @param event The event to edit
     */
    public AddEditEventWindow(Event event) {

        this(true, event, null);
        setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/gui/resources/logo.png")));
    }

    /**
     * Creates new form to add/edit a event
     *
     * @param isEdit whether we want to edit or create an event
     * @param event the event to edit
     * @param track the track the event should belong to
     */
    private AddEditEventWindow(boolean isEdit, Event event, Track track) {
        if (!isEdit && track == null) {
            throw new NullPointerException();
        }

        if (isEdit && event == null) {
            throw new NullPointerException();
        }

        this.isEdit = isEdit;
        textAreaVisible = true;
        this.event = event;

        initComponents();
        map.getMainMap().addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent evt) {
                mapMouseClicked(evt);
            }
        });

        if (track != null) {
            this.track = track;
        } else if (event != null) {
            this.track = event.getTrack();
        }

        mapManager = new MapManager(map);
        mapManager.drawTrack(this.track.getTrackList());

        timePicker.setDate(this.track.getDate(), true);

        if (isEdit) {
            setTitle(ResourceBundle.getBundle("gui/guilabels").getString("editevent"));
            setContent();
        } else {
            setTitle(ResourceBundle.getBundle("gui/guilabels").getString("addevent"));
        }
    }

    private void setContent() {
        textName.setText(event.getEventName());
        timePicker.setDate(event.getEventDate(), true);
        textLatitude.setText(Float.toString(event.getPos().getLanDeg()));
        textLongitude.setText(Float.toString(event.getPos().getLonDeg()));

        setCoords(event.getPos().toGeoPosition());

        switch (event.getEventType()) {
            case Image:
                comboBoxType.setSelectedIndex(COMBO_PICTURE_SELECTED_INDEX);
                textFilePath.setText(((ImageEvent) event).getImage().getPath());
                break;
            case Text:
                comboBoxType.setSelectedIndex(COMBO_TEXT_SELECTED_INDEX);
                textAreaDescription.setText(((TextEvent) event).getText());
                break;
            case Video:
                comboBoxType.setSelectedIndex(COMBO_VIDEO_SELECTED_INDEX);
                textFilePath.setText(((VideoEvent) event).getVideo().getPathName());
                break;
        }

        comboBoxType.setEnabled(false);
    }

    private void toggleTextArea() {
        GridBagLayout layout = (GridBagLayout) getContentPane().getLayout();

        if (textAreaVisible) {
            panelContent.setMinimumSize(new Dimension(0, 0));
            panelContent.setMaximumSize(new Dimension(0, 0));
            panelContent.setPreferredSize(new Dimension(0, 0));

            GridBagConstraints invisibleConstraints = new GridBagConstraints();
            invisibleConstraints.gridx = 0;
            invisibleConstraints.gridy = 3;
            invisibleConstraints.gridwidth = 2;
            layout.setConstraints(panelContent, invisibleConstraints);

            jPanel1.setMinimumSize(new Dimension(100, 20));
            jPanel1.setMaximumSize(new Dimension(Integer.MAX_VALUE, 20));
            jPanel1.setPreferredSize(new Dimension(100, 20));

            GridBagConstraints constraints = new GridBagConstraints();
            constraints.gridx = 1;
            constraints.gridy = 2;
            constraints.gridwidth = 1;
            constraints.fill = GridBagConstraints.HORIZONTAL;
            constraints.anchor = java.awt.GridBagConstraints.LINE_START;
            constraints.weightx = 1.0;
            constraints.insets = new Insets(5, 5, 5, 5);

            layout.setConstraints(jPanel1, constraints);
        } else {
            jPanel1.setMinimumSize(new Dimension(0, 0));
            jPanel1.setMaximumSize(new Dimension(0, 0));
            jPanel1.setPreferredSize(new Dimension(0, 0));

            GridBagConstraints invisibleConstraints = new GridBagConstraints();
            invisibleConstraints.gridx = 1;
            invisibleConstraints.gridy = 2;
            layout.setConstraints(jPanel1, invisibleConstraints);

            panelContent.setMinimumSize(new Dimension(23, 23));
            panelContent.setMaximumSize(new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE));
            panelContent.setPreferredSize(new Dimension(0, 0));

            GridBagConstraints constraints = new GridBagConstraints();
            constraints.gridx = 0;
            constraints.gridy = 3;
            constraints.gridwidth = 2;
            constraints.fill = java.awt.GridBagConstraints.BOTH;
            constraints.anchor = java.awt.GridBagConstraints.LINE_START;
            constraints.weightx = 1.0;
            constraints.weighty = 1.0;
            constraints.insets = new Insets(5, 5, 5, 5);

            layout.setConstraints(panelContent, constraints);
        }

        textAreaVisible = !textAreaVisible;

        pack();
    }

    private void setCoords(GeoPosition pos) {
        textLatitude.setText(Double.toString(pos.getLatitude()));
        textLongitude.setText(Double.toString(pos.getLongitude()));

        // draw marker on map
        mapManager.paintGPSPoint(pos);
    }

    public void mapMouseClicked(MouseEvent evt) {
        GeoPosition pos = MapUtils.getGeoPointByClickEvent(evt.getPoint(), map.getMainMap());

        setCoords(pos);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jLabel1 = new javax.swing.JLabel();
        comboBoxType = new javax.swing.JComboBox();
        panelContent = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        textAreaDescription = new javax.swing.JTextArea();
        labelDesc = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        textLatitude = new javax.swing.JTextField();
        textLongitude = new javax.swing.JTextField();
        timePicker = new gui.picker.TimeFieldPicker();
        jLabel5 = new javax.swing.JLabel();
        buttonCancel = new javax.swing.JButton();
        buttonSave = new javax.swing.JButton();
        map = new org.jdesktop.swingx.JXMapKit();
        jPanel1 = new javax.swing.JPanel();
        textFilePath = new javax.swing.JTextField();
        btnGetFile = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        textName = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setModal(true);
        setPreferredSize(new java.awt.Dimension(430, 600));
        getContentPane().setLayout(new java.awt.GridBagLayout());

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("gui/guilabels"); // NOI18N
        jLabel1.setText(bundle.getString("type")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(7, 5, 5, 5);
        getContentPane().add(jLabel1, gridBagConstraints);

        comboBoxType.setModel(new javax.swing.DefaultComboBoxModel(new String[] { bundle.getString("text"), bundle.getString("image"), bundle.getString("videowc") }));
        comboBoxType.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboBoxTypeActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(7, 5, 5, 7);
        getContentPane().add(comboBoxType, gridBagConstraints);

        panelContent.setPreferredSize(new java.awt.Dimension(0, 0));
        panelContent.setLayout(new java.awt.CardLayout());

        textAreaDescription.setColumns(20);
        textAreaDescription.setLineWrap(true);
        textAreaDescription.setRows(5);
        jScrollPane1.setViewportView(textAreaDescription);

        panelContent.add(jScrollPane1, "card2");

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(panelContent, gridBagConstraints);

        labelDesc.setText(bundle.getString("description")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(labelDesc, gridBagConstraints);

        jLabel3.setText(bundle.getString("latitude")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(jLabel3, gridBagConstraints);

        jLabel4.setText(bundle.getString("longitude")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(jLabel4, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(textLatitude, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(textLongitude, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(timePicker, gridBagConstraints);

        jLabel5.setText(bundle.getString("time")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(jLabel5, gridBagConstraints);

        buttonCancel.setText(bundle.getString("cancel")); // NOI18N
        buttonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonCancelActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(5, 7, 7, 5);
        getContentPane().add(buttonCancel, gridBagConstraints);

        buttonSave.setText(bundle.getString("save")); // NOI18N
        buttonSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonSaveActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 7, 7);
        getContentPane().add(buttonSave, gridBagConstraints);

        map.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(209, 209, 209)));
        map.setDefaultProvider(org.jdesktop.swingx.JXMapKit.DefaultProviders.OpenStreetMaps);
        map.setMiniMapVisible(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 0.5;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(map, gridBagConstraints);

        jPanel1.setMaximumSize(new java.awt.Dimension(0, 0));
        jPanel1.setMinimumSize(new java.awt.Dimension(0, 0));
        jPanel1.setPreferredSize(new java.awt.Dimension(0, 0));
        jPanel1.setLayout(new java.awt.BorderLayout());

        textFilePath.setEditable(false);
        jPanel1.add(textFilePath, java.awt.BorderLayout.CENTER);

        btnGetFile.setText("...");
        btnGetFile.setMinimumSize(new java.awt.Dimension(25, 23));
        btnGetFile.setPreferredSize(new java.awt.Dimension(25, 23));
        btnGetFile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGetFileActionPerformed(evt);
            }
        });
        jPanel1.add(btnGetFile, java.awt.BorderLayout.LINE_END);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        getContentPane().add(jPanel1, gridBagConstraints);

        jLabel2.setText(bundle.getString("name")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(jLabel2, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(textName, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void comboBoxTypeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboBoxTypeActionPerformed
        if (comboBoxType.getSelectedIndex() != COMBO_TEXT_SELECTED_INDEX && textAreaVisible) {
            toggleTextArea();
        } else if (comboBoxType.getSelectedIndex() == COMBO_TEXT_SELECTED_INDEX && !textAreaVisible) {
            toggleTextArea();
        }

        switch (comboBoxType.getSelectedIndex()) {
            case COMBO_TEXT_SELECTED_INDEX:
                labelDesc.setText(ResourceBundle.getBundle("gui/guilabels").getString("description"));
                break;
            case COMBO_PICTURE_SELECTED_INDEX:
                labelDesc.setText(ResourceBundle.getBundle("gui/guilabels").getString("picture"));
                break;
            case COMBO_VIDEO_SELECTED_INDEX:
                labelDesc.setText(ResourceBundle.getBundle("gui/guilabels").getString("video"));
                break;
        }
    }//GEN-LAST:event_comboBoxTypeActionPerformed

    private void buttonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCancelActionPerformed
        this.dispose();
    }//GEN-LAST:event_buttonCancelActionPerformed

    private boolean checkMustHaveFields() {
        boolean isOK = true;

        if (textName.getText().isEmpty()) {
            isOK = false;
        }

        if (textLatitude.getText().isEmpty()) {
            isOK = false;
        }

        if (textLongitude.getText().isEmpty()) {
            isOK = false;
        }

        if (!isOK) {
            JOptionPane.showMessageDialog(this, ResourceBundle.getBundle("gui/guilabels").getString("fillallfields"), ResourceBundle.getBundle("gui/guilabels").getString("error"), JOptionPane.WARNING_MESSAGE);
        } else {
            try {
                Float.parseFloat(textLatitude.getText());
                Float.parseFloat(textLongitude.getText());
            } catch (Exception e) {
                isOK = false;
                JOptionPane.showMessageDialog(this, ResourceBundle.getBundle("gui/guilabels").getString("NaNCoords"), ResourceBundle.getBundle("gui/guilabels").getString("error"), JOptionPane.WARNING_MESSAGE);
            }
        }

        if (isOK) {
            Calendar trackBegin = track.getDate();
            Calendar trackEnd = track.getDate();
            trackEnd.add(Calendar.MILLISECOND, (int) track.getMetadata().getTime());

            Calendar currentChosenTime = timePicker.getDate();

            if (currentChosenTime.compareTo(trackBegin) < 0
                    || currentChosenTime.compareTo(trackEnd) > 0) {
                isOK = false;
                JOptionPane.showMessageDialog(this, ResourceBundle.getBundle("gui/guilabels").getString("timenotintrack"), ResourceBundle.getBundle("gui/guilabels").getString("error"), JOptionPane.WARNING_MESSAGE);
            } else {
                switch (comboBoxType.getSelectedIndex()) {
                    case COMBO_PICTURE_SELECTED_INDEX:
                    case COMBO_VIDEO_SELECTED_INDEX:
                        if (textFilePath.getText().isEmpty()) {
                            isOK = false;
                            JOptionPane.showMessageDialog(this, ResourceBundle.getBundle("gui/guilabels").getString("noFilePath"), ResourceBundle.getBundle("gui/guilabels").getString("error"), JOptionPane.WARNING_MESSAGE);
                        }
                        break;
                    case COMBO_TEXT_SELECTED_INDEX:
                        if (textAreaDescription.getText().isEmpty()) {
                            isOK = false;
                            JOptionPane.showMessageDialog(this, ResourceBundle.getBundle("gui/guilabels").getString("noText"), ResourceBundle.getBundle("gui/guilabels").getString("error"), JOptionPane.WARNING_MESSAGE);
                        }
                        break;
                }
            }
        }

        return isOK;
    }

    private void buttonSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonSaveActionPerformed
        if (checkMustHaveFields()) {
            try {
                if (event == null) {
                    switch (comboBoxType.getSelectedIndex()) {
                        case COMBO_PICTURE_SELECTED_INDEX:
                            if (this.image == null) {
                                this.image = new Image(textFilePath.getText());
                            }

                            this.image.importPicture();

                            event = new ImageEvent(track,
                                    textName.getText(),
                                    timePicker.getDate().getTimeInMillis(),
                                    new GeoPoint(Float.parseFloat(textLatitude.getText()), Float.parseFloat(textLongitude.getText())),
                                    this.image);
                            break;
                        case COMBO_TEXT_SELECTED_INDEX:
                            event = new TextEvent(track,
                                    textName.getText(),
                                    timePicker.getDate().getTimeInMillis(),
                                    new GeoPoint(Float.parseFloat(textLatitude.getText()), Float.parseFloat(textLongitude.getText())),
                                    textAreaDescription.getText());
                            break;
                        case COMBO_VIDEO_SELECTED_INDEX:
                            event = new VideoEvent(track,
                                    textName.getText(),
                                    timePicker.getDate().getTimeInMillis(),
                                    new GeoPoint(Float.parseFloat(textLatitude.getText()), Float.parseFloat(textLongitude.getText())),
                                    new Video(textFilePath.getText()));
                            break;
                    }

                    event.setParentTrack(track);
                } else {
                    event.setDescription(textName.getText());
                    event.setTimestamp(timePicker.getDate().getTimeInMillis());
                    event.setPos(new GeoPoint(Float.parseFloat(textLatitude.getText()), Float.parseFloat(textLongitude.getText())));

                    switch (comboBoxType.getSelectedIndex()) {
                        case COMBO_PICTURE_SELECTED_INDEX:
                            ((ImageEvent) event).setImage(this.image == null ? new Image(textFilePath.getText()) : this.image);
                            break;
                        case COMBO_TEXT_SELECTED_INDEX:
                            ((TextEvent) event).setText(textAreaDescription.getText());
                            break;
                        case COMBO_VIDEO_SELECTED_INDEX:
                            ((VideoEvent) event).setVideo(new Video(textFilePath.getText()));
                            break;
                    }
                }

                if (isEdit) {
                    DBEventWorker worker = new DBEventWorker(DBEventWorker.Type.UpdateEvent, event);
                    worker.execute();
                    this.dispose();
                } else {
                    DBEventWorker worker = new DBEventWorker(DBEventWorker.Type.SaveEvent, event);
                    worker.execute();
                    this.setVisible(false);
                }

            } catch (Exception e) {
                JOptionPane.showMessageDialog(this,
                        ResourceBundle.getBundle("gui/guilabels").getString("errorsavingevent"),
                        ResourceBundle.getBundle("gui/guilabels").getString("error"),
                        JOptionPane.ERROR_MESSAGE);
            }
        }
    }//GEN-LAST:event_buttonSaveActionPerformed

    private void btnGetFileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGetFileActionPerformed
        final JFileChooser chooser = new JFileChooser(ResourceBundle.getBundle("gui/guilabels").getString("choosefile"));
        chooser.setDialogType(JFileChooser.OPEN_DIALOG);
        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        chooser.setFileFilter(new FileFilter() {
            public boolean accept(File f) {
                String[] accept = comboBoxType.getSelectedIndex() == COMBO_PICTURE_SELECTED_INDEX
                        ? new String[]{".png", ".jpeg", ".jpg"}
                        : new String[]{".avi", ".flv", ".mpeg"};

                for (String s : accept) {
                    if ((f.getName().toLowerCase().endsWith(s) && f.isFile()) || f.isDirectory()) {
                        return true;
                    }

                }

                return false;
            }

            public String getDescription() {
                return comboBoxType.getSelectedIndex() == COMBO_PICTURE_SELECTED_INDEX
                        ? ResourceBundle.getBundle("gui/guilabels").getString("imagefiletype")
                        : ResourceBundle.getBundle("gui/guilabels").getString("videofiletype");
            }
        });

        chooser.setMultiSelectionEnabled(false);
        if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
            textFilePath.setText(chooser.getSelectedFile().getPath());

            if (comboBoxType.getSelectedIndex() == COMBO_PICTURE_SELECTED_INDEX) {
                try {
                    image = new Image(chooser.getSelectedFile().getPath());

                    EXIFData exif = image.getExif();
                    if (exif != null) {
                        if (exif.getDateTimeOriginal() != null) {
                            Calendar picTime = Calendar.getInstance();
                            picTime.setTime(exif.getDateTimeOriginal());

                            Calendar cal = timePicker.getDate();
                            cal.set(Calendar.HOUR_OF_DAY, picTime.get(Calendar.HOUR_OF_DAY));
                            cal.set(Calendar.MINUTE, picTime.get(Calendar.MINUTE));
                            timePicker.setDate(cal, true);
                        }

                        if (exif.getGeoPoint() != null) {
                            setCoords(new GeoPosition(exif.getGeoPoint().getLanDeg(), exif.getGeoPoint().getLonDeg()));
                        }
                    }
                } catch (IOException ex) {
                    textFilePath.setText("");
                    JOptionPane.showMessageDialog(this,
                            ResourceBundle.getBundle("gui/guilabels").getString("couldntloadimage") + "\n" + ex.getLocalizedMessage(),
                            ResourceBundle.getBundle("gui/guilabels").getString("error"),
                            JOptionPane.ERROR_MESSAGE);
                }
            }
        }
    }//GEN-LAST:event_btnGetFileActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnGetFile;
    private javax.swing.JButton buttonCancel;
    private javax.swing.JButton buttonSave;
    private javax.swing.JComboBox comboBoxType;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel labelDesc;
    private org.jdesktop.swingx.JXMapKit map;
    private javax.swing.JPanel panelContent;
    private javax.swing.JTextArea textAreaDescription;
    private javax.swing.JTextField textFilePath;
    private javax.swing.JTextField textLatitude;
    private javax.swing.JTextField textLongitude;
    private javax.swing.JTextField textName;
    private gui.picker.TimeFieldPicker timePicker;
    // End of variables declaration//GEN-END:variables
}
